package com.zygh.lz.service.role;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zygh.lz.dao.AccountMapper;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.dao.role.SysMenuMapper;
import com.zygh.lz.entity.Account;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.role.SysMenu;
import com.zygh.lz.service.StaffService;
import com.zygh.lz.stapprove.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> {

    @Autowired
    StaffService staffService;

    @Resource
    StaffMapper staffMapper;
    @Resource
    SysMenuMapper sysMenuMapper;
@Resource
private AccountMapper accountMapper;

    public List<SysMenuDto> getCurrentUserNav() {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
       // Staff sysUser = staffService.getOne(new QueryWrapper<Staff>()
          //      .eq("staff_tel", username));
        Account sysUser = accountMapper.selectOne(Wrappers.<Account>lambdaQuery()
                .eq(Account::getStaffTel, username));
        List<Long> menuIds = accountMapper.getNavMenuIds(sysUser.getSysStaffId().longValue());
        if (menuIds.size() == 0) {
            return null;
        }
        // List<SysMenu> menus = this.listByIds(menuIds);
        List<SysMenu> menus = sysMenuMapper.selectList(Wrappers.<SysMenu>lambdaQuery()
                .in(SysMenu::getId, menuIds).orderByAsc(SysMenu::getOrderNum));
        // 转树状结构
        List<SysMenu> menuTree = buildTreeMenu(menus);

        // 实体转DTO
        return convert(menuTree);
    }


    public List<SysMenu> tree() {
        // 获取所有菜单信息
        List<SysMenu> sysMenus = this.list(new QueryWrapper<SysMenu>().orderByAsc("order_num"));

        // 转成树状结构
        return buildTreeMenu(sysMenus);
    }

    public List<SysMenu> treeOpen() {
        // 获取所有菜单信息
        List<SysMenu> sysMenus = this.list(new QueryWrapper<SysMenu>()
                .eq("statu", 1)
                .orderByAsc("order_num"));

        // 转成树状结构
        return buildTreeMenu(sysMenus);
    }


    private List<SysMenuDto> convert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuDto dto = new SysMenuDto();

            dto.setId(m.getId());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setIcon(m.getIcon());
            dto.setIcons(m.getIcons());
            dto.setComponent(m.getComponent());
            dto.setPath(m.getPath());
            dto.setType(m.getType());
            dto.setShowWay(m.getShowWay());
            dto.setBottom(m.getBottom());
            dto.setParentId(m.getParentId());

            if (m.getChildren().size() > 0) {

                // 子节点调用当前方法进行再次转换
                dto.setChildren(convert(m.getChildren()));
            }

            menuDtos.add(dto);
        });

        return menuDtos;
    }

    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {

        List<SysMenu> finalMenus = new ArrayList<>();

        // 先各自寻找到各自的孩子
        for (SysMenu menu : menus) {

            for (SysMenu e : menus) {
                if (menu.getId().equals(e.getParentId())) {
                    menu.getChildren().add(e);
                }
            }

            // 提取出父节点
            if (menu.getParentId() == 0L) {
                finalMenus.add(menu);
            }
        }

        return finalMenus;
    }
}
